class Solution {
    public int totalFruit(int[] fruits) {
        int left=0,right=0,kind=0,ret=0;
        int n=fruits.length;
        int []nums=new int[n];
        while(right<n){
            if(nums[fruits[right]]==0) kind++;
            nums[fruits[right]]++;
            while(kind>2){
                nums[fruits[left]]--;
                if(nums[fruits[left++]]==0) kind--;
            }
            ret=Math.max(ret,right-left+1);
            right++;
        }
        
        return ret;
    }
}